﻿#include "mathutils.h"
#include <QDebug>

MathUtils::MathUtils()
{
//    mat(10,10);
}

QList<double> MathUtils::LinearInterpolation(QList<double> xAxis, const QList<QPointF> &dataPoints)
{
    QList<double> ret;
    for(int i=0;i<xAxis.count();i++)
    {
        ret.append(linearInterpolation(xAxis.at(i),dataPoints));
    }
    return ret;
}

double MathUtils::linearInterpolation(double x_interpolate, const QList<QPointF> &dataPoints)
{
    int n = dataPoints.size();
    if (n < 2)
        return 0.0;

    for (int i = 0; i < n - 1; ++i)
    {
        if (x_interpolate >= dataPoints[i].x() && x_interpolate <= dataPoints[i + 1].x())
        {
            double x1 = dataPoints[i].x();
            double x2 = dataPoints[i + 1].x();
            double y1 = dataPoints[i].y();
            double y2 = dataPoints[i + 1].y();

            return y1 + (x_interpolate - x1) * (y2 - y1) / (x2 - x1);
        }
    }
    qDebug()<<__FUNCTION__<<__LINE__<<"Error!";
    return 0.0; // Default value if interpolation point is out of range
}
